local scrSize = tgm.ScreenSize

local board_x = scrSize * 0.02
local board_y = scrSize * 0.02
local board_w = scrSize * 0.96
local board_h = scrSize * 0.96

local tilesize = board_h / 8
local tileoutline = tilesize / 10

local symbols = {
	{"h", "d",Color(255,255,255,255)},
	{"h", "d",Color(0,0,0,255)}
}

GAME.MouseoverTile = false

function GAME:RenderBoard ()
	local mx,my = self:MousePos()
	self.MouseoverTile = false
	for x,row in pairs (self.Board) do
		for y,tile in pairs (row) do
			local shade = 100
			if (x+y)/2 == math.floor((x+y)/2) then
				shade = 150
			end
			if mx > board_x + (x-1) * tilesize
			and mx < board_x + (x-1) * tilesize + tilesize
			and my > board_y + (y-1) * tilesize
			and my < board_y + (y-1) * tilesize + tilesize
			then
				self.MouseoverTile = {x,y}
				shade = shade + 25
			end
			surface.SetDrawColor (shade, shade, shade, 255)
			surface.DrawRect (board_x + (x-1) * tilesize, board_y + (y-1) * tilesize, tilesize + 1, tilesize + 1)
			if tile then
				draw.SimpleText(symbols[tile[1]][tile[2]], "Draughts120", board_x + (x-1) * tilesize + tilesize * 0.5, board_y + (y-1) * tilesize, symbols[tile[1]][3], TEXT_ALIGN_CENTER)
			end
			local outlineCol = false
			if self.MouseoverTile and self.MouseoverTile[1] == x and self.MouseoverTile[2] == y then
				outlineCol = {0,0,0}
			elseif self.SelectedPiece and self.SelectedPiece[1] == x and self.SelectedPiece[2] == y then
				outlineCol = {0,0,255}
			elseif self.PossibleTiles then
				for k,v in pairs (self.PossibleTiles) do
					if v[1] == x and v[2] == y then
						outlineCol = {0,255,0}
					end
				end
			end
			if outlineCol then
				surface.SetDrawColor (outlineCol[1], outlineCol[2], outlineCol[3], 255)
				surface.DrawRect (board_x + (x-1) * tilesize, board_y + (y-1) * tilesize, tilesize, tileoutline)
				surface.DrawRect (board_x + (x-1) * tilesize, board_y + (y-1) * tilesize, tileoutline, tilesize)
				surface.DrawRect (board_x + (x-1) * tilesize + tilesize - tileoutline, board_y + (y-1) * tilesize, tileoutline + 1, tilesize)
				surface.DrawRect (board_x + (x-1) * tilesize, board_y + (y-1) * tilesize  + tilesize - tileoutline, tilesize, tileoutline + 1)
			end
		end
	end
end

function GAME:OnSelect ()
	if not self.Opponent then return end --UNCOMMENT
	if not self.MouseoverTile then return end
	local x,y = self.MouseoverTile[1], self.MouseoverTile[2]
	local piece = self.Board[x][y]
	if piece then
		if piece[1] != self.PlayingAs then return end --UNCOMM
		self.SelectedPiece = {x,y}
		local posTbl = self:GetPossibleMoves (x,y,piece[1],piece[2])
		self.PossibleTiles = posTbl
	elseif self.SelectedPiece then
		if self.Turn != self.PlayingAs then return end --UNCOMM
		local sx,sy = self.SelectedPiece[1], self.SelectedPiece[2]
		local sPiece = self.Board[sx][sy]
		if self.Turn != sPiece[1] then return end
		if piece then return end
		local legit = false
		for k,v in pairs (self.PossibleTiles) do
			if v[1] == x and v[2] == y then
				legit = true
			end
		end
		if legit then
			local anotherTurn = self:DoMove (sx,sy,x,y,true)
			if not anotherTurn then
				if self.Turn == 1 then self.Turn = 2 else self.Turn = 1 end
			end
		end
	end
end
